package com.xhh.edcservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhh.commonutils.R;
import com.xhh.edcservice.entity.EduTeacher;
import com.xhh.edcservice.entity.vo.TeacherQuery;
import com.xhh.edcservice.service.EduTeacherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author wei-xhh
 * @since 2020-09-26
 */
@RestController
@RequestMapping("/edcservice/teacher")
@Tag(name = "讲师模块")
public class EduTeacherController {

    @Autowired
    EduTeacherService eduTeacherService;

    @Operation(
            summary = "查询所有讲师",
            description = "查询所有讲师",
            responses = {
                    @ApiResponse(
                            description = "返回值对象",
                            content = @Content(
                                    mediaType = "application/json",
                                    schema = @Schema(implementation = EduTeacher.class)
                            )
                    )
            })
    @GetMapping("/findAll")
    public R findAll() {
        List<EduTeacher> list = eduTeacherService.list();
        return R.ok().data("items", list);
    }

    @Operation(
            summary = "逻辑删除讲师",
            description = "逻辑删除讲师",
            parameters = {
                    @Parameter(name = "id", description = "删除id", required = true)
            }
    )
    @DeleteMapping("/{id}")
    public R removeTeacher(@PathVariable(name = "id") String id) {
        boolean flag = eduTeacherService.removeById(id);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 分页查询讲师的方法
     *
     * @param current 当前页
     * @param limit   大小
     * @return 分页结果
     */
    @Operation(
            summary = "分页查询讲师",
            description = "分页查询讲师",
            parameters = {
                    @Parameter(name = "current", description = "当前页", required = true),
                    @Parameter(name = "limit", description = "大小", required = true)
            }
    )
    @GetMapping("/pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current,
                             @PathVariable long limit) {
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        //调用方法实现分页
        eduTeacherService.page(pageTeacher, null);

        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();

        return R.ok().data("total", total).data("rows", records);

    }

    /**
     * 条件查询带分页
     */
    @PostMapping("/pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery) {
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);

        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();

        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //判断条件是否为空，如果不空拼接条件
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create", end);
        }

        eduTeacherService.page(pageTeacher, wrapper);

        //总记录数
        long total = pageTeacher.getTotal();
        //数据list集合
        List<EduTeacher> records = pageTeacher.getRecords();

        return R.ok().data("total", total).data("rows", records);
    }


    /**
     * 添加讲师接口
     */
    @PostMapping(value = "/addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        //TODO 使用Swagger3不知道怎么指定MediaType
        boolean save = eduTeacherService.save(eduTeacher);
        return save ? R.ok() : R.error();
    }
}

